<div id="db_dialog" title="as" style="display: none;">
	<input id="DbDialog_equipment_id" type="text" name="equipment-id" style="display: none;"/>
	<form id="add_edit_db">
		<table style="width: 100%"> 
			<tr>
				<td> Zadajte názov vybavenia </td>
				<td> <input id="DbDialog_equipment_name" type="text" style="width: 100%" name="equipment-name"/> </td>
			</tr>
			<tr>
				<td> Zadajte dátum prijatia </td>
				<td> <input id="DbDialog_date_added" type="text" style="width: 100%" class="datepicker" name="date-added"/> </td>
			</tr>
			<tr>
				<td> Zadajte kategóriu pre vybavenie </td>
				<td> 
					<select id="DbDialog_category_id" style="width: 100%" name="equipment-category">
					</select>
				</td>
			</tr>
			<tr>
				<td> Zadajte meno zodpovednej osoby </td>
				<td> <input id="DbDialog_person_name" type="text" style="width: 100%" name="person-name"/> </td>
			</tr>
			<tr>
				<td> Zadajte miestnosť </td>
				<td> <input id="DbDialog_room_name" type="text" style="width: 100%" name="room-name"/> </td>
			</tr>
			<tr>
				<td> Zadajte podrobnosti </td>
				<td> <textarea id="DbDialog_info" name="info" style="width: 100%"></textarea> </td>
			</tr>
		</table>
	</form>
</div>

<div id="delete-dialog" style="display: none;">
	Skutočne chcete vymazať túto položku?
</div>

<script type="text/javascript">
	function GetEquipmentCategories(){
		$.ajax({
			url: AJAX_URL,
			data:{
				controller: "home/user",
				action: "getCategories"
			},
			success: function(data){
				var select = $("#DbDialog_category_id");
				for(var i in data){
					var option = $("<option></option>");
					var lifetime = data[i].lifetime;
					if(lifetime == 1){
						lifetime += " rok"
					} else if(lifetime > 1 && lifetime < 5){
						lifetime += " roky";
					} else {
						lifetime += " rokov";
					}
					
					var name = data[i].category_name + " (" + lifetime + ")";
					option.html(name);
					option.val(data[i].category_id);
					option.appendTo(select);
				}
			}
		});
	}

	function RegisterAutoComplete(target, actionName){
		$.ajax({
			url: AJAX_URL,
			data:{
				controller: "home/user",
				action: actionName
			},
			success: function(data){
				target.autocomplete({
					source: data.result,
				});		
			},
		});
	}
	
	function DeleteDialogOpen(id){
		var dialog = $("#delete-dialog").dialog({
			modal: true,
			title: "Potvrdiť akciu",
			height: 110,
			width: 500,
			buttons: {
				"Áno" : function(){
					$.ajax({
						data: {
							controller: "home/user",
							action: "deleteEquipment",
							id: id
						},
						success: function(data){
							if (data.success){
								dialog.dialog("close");	
								location.reload();	
							} else {
								alert(data.message);
								dialog.dialog("close");
							}
						}
					});
				},
				"Nie" : function(){
					$(this).dialog("close");
				},
				"Zrušiť" : function(){
					$(this).dialog("close");
				}
			}
		});
	}
	
	function AddDialogOpen(){
		$("#db_dialog").find("input").val("");
		$("#db_dialog").find("textarea").val("");
		$("#db_dialog").dialog({
			title: "Pridať novú položku do databázy",
			width: 500,
			buttons: {
				"Pridať" : function(){
					$.ajax({
						url: AJAX_URL,
						data:{
							controller: "home/user",
							action: "addEquipment",
							"equipment-name" : $("#DbDialog_equipment_name").val(),
							"date-added" 	 : $("#DbDialog_date_added").val(),
							"person-name" 	 : $("#DbDialog_person_name").val(),
							"room-name" 	 : $("#DbDialog_room_name").val(),
							"info" 			 : $("#DbDialog_info").val(),
							"category_id"    : $("#DbDialog_category_id").val()
						},
						success: function(data){
							if (data.success){
								$("#db_dialog").dialog("close");
								formValidator.resetForm();
							} else {
								alert(data.message);
							}
						},	
					})
				},
				"Zrušiť" : function(){
					$(this).dialog("close");
				}
			}
		});
	}
	
	function EditDialogOpen(data){
		$("#db_dialog").dialog({
			title: "Upraviť položku " + data.equipment_name,
			width: 500,
			buttons: {
				"Upraviť" : function(){
					$.ajax({
						url: AJAX_URL,
						data:{
							controller: "home/user",
							action: "editEquipment",
							"equipment-id"   : $("#DbDialog_equipment_id").val(),
							"equipment-name" : $("#DbDialog_equipment_name").val(),
							"date-added" 	 : $("#DbDialog_date_added").val(),
							"person-name" 	 : $("#DbDialog_person_name").val(),
							"room-name" 	 : $("#DbDialog_room_name").val(),
							"info" 			 : $("#DbDialog_info").val(),
							"category_id"    : $("#DbDialog_category_id").val()
						},
						success: function(data){
							if (data.success){
								$("#db_dialog").dialog("close");
								location.reload();
							} else {
								alert(data.message);
							}
						},	
					})
				},
				"Zrušiť" : function(){
					$(this).dialog("close");
				}
			}
		});
		$("#DbDialog_equipment_id").val(data.equipment_id);
		$("#DbDialog_equipment_name").val(data.equipment_name);
		$("#DbDialog_date_added").val(data.date_added);
		$("#DbDialog_person_name").val(data.person_name);
		$("#DbDialog_room_name").val(data.room_name);
		$("#DbDialog_info").val(data.info_full);
		$("#DbDialog_category_id").val(data.category_id);
	}
	
	$(function(){
		RegisterAutoComplete($("#DbDialog_person_name"), "GetPersons");
		RegisterAutoComplete($("#DbDialog_room_name"), "GetRooms");
		GetEquipmentCategories();
		
		var req_msg = "Táto položka je povinná";
		var ml_msg = "Táto položka musí byť dlhá minimálne 3 znaky";
		
		
		var formValidator = $("#add_edit_db").validate({
			rules: {
    			"equipment-name": {
    				required: true,
    				minlength: 3
    			},
    			"date-added": {
    				required: true
    			},
    			"person-name": {
    				required: true,
    				minlength: 3
    			},
    			"room-name": {
    				required: true,
    				minlength: 3
    			}
			},
			messages: {
				"equipment-name": {
    				required: req_msg,
    				minlength: ml_msg
    			},
    			"date-added": {
    				required: req_msg
    			},
    			"person-name": {
    				required: req_msg,
    				minlength: ml_msg
    			},
    			"room-name": {
    				required: req_msg,
    				minlength: ml_msg
    			}
			},
			submitHandler: function(form) {
				
			}
		});
	})	
	
	
</script>